package com.ytkj.ygAssist.view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.LookAndFeel;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import com.ytkj.ygAssist.main.YunGouLoginServer;
import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.tools.Config;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.tools.MyLog;
import com.ytkj.ygAssist.view.myView.HintDialog;
import com.ytkj.ygAssist.view.myView.ShowMyMenu;
import javax.swing.SwingConstants;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Cursor;

/*
 * 提前揭晓
 */
public class Foreknow extends JPanel {
	private static final long serialVersionUID = 1L;
	public int selectNum = 0;
	public long beginTime = 0;
	public Thread thread = null;
	public JFrameListeningInterface foreknowInterface;
	public JTextField goodsIdTextField;// 商品id输入框
	public JTextField periodTextField;// 期数输入框
	public JLabel selectInfoLabel;
	public JTable table;
	public JScrollPane scrollPane;
	public JCheckBox verifyWinCheckBox;// 验证中奖选择
	public JComboBox<String> selectNumComboBox;// 查询数量
	public JLabel goodsNameLblid;
	public JButton selectButton;// 查询按钮
	public JComboBox<String> latelyPeriodComboBox;// 最近期数选择

	public Foreknow() {
		setLayout(null);

		JLabel lblid = new JLabel("商品ID");
		lblid.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblid.setBounds(35, 21, 49, 20);
		add(lblid);

		goodsIdTextField = new JTextField(Config.defaultGoodsIdTextFieldString);
		goodsIdTextField.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				if (arg0.getButton() == MouseEvent.BUTTON3) {// 右键点击
					ShowMyMenu.ShowRightClickMenu(goodsIdTextField, arg0.getX(), arg0.getY());
				}
			}
		});
		goodsIdTextField.setHorizontalAlignment(SwingConstants.CENTER);
		goodsIdTextField.setBounds(92, 15, 100, 30);
		goodsIdTextField.setColumns(10);
		add(goodsIdTextField);
		JButton comboBox_3 = new JButton();
		comboBox_3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (goodsIdTextField.isEnabled()) {
					ShowMyMenu.ShowGoodsIDMenu(goodsIdTextField);
				}
			}
		});
		comboBox_3.setCursor(new Cursor(Cursor.HAND_CURSOR));
		comboBox_3.setBorderPainted(false);
		comboBox_3.setBounds(goodsIdTextField.getWidth() - 18, 0, 18, 28);
		comboBox_3.setUI(new BasicButtonUI() {
			@Override
			protected void installDefaults(AbstractButton b) {
				LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
			}

			@Override
			public void paint(Graphics g, JComponent c) {
				g.drawImage(new ImageIcon(IntelligentMonitoring.class.getResource("/images/mainJFrame/xialaanniu.png"))
						.getImage(), 0, 2, c.getWidth() - 2, c.getHeight() - 2, c);
			}
		});
		goodsIdTextField.add(comboBox_3);

		JLabel label = new JLabel("期数");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label.setBounds(214, 21, 33, 15);
		add(label);

		periodTextField = new JTextField();
		periodTextField.setHorizontalAlignment(SwingConstants.CENTER);
		periodTextField.setBounds(261, 15, 100, 30);
		add(periodTextField);
		periodTextField.setColumns(10);
		periodTextField.setEnabled(false);
		JButton comboBox_4 = new JButton();
		comboBox_4.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (periodTextField.isEnabled()) {
					ShowMyMenu.ShowGoodsPeriodMenu(periodTextField, goodsIdTextField.getText());
				}
			}
		});
		comboBox_4.setCursor(new Cursor(Cursor.HAND_CURSOR));
		comboBox_4.setBorderPainted(false);
		comboBox_4.setBounds(goodsIdTextField.getWidth() - 18, 0, 18, 28);
		comboBox_4.setUI(new BasicButtonUI() {
			@Override
			protected void installDefaults(AbstractButton b) {
				LookAndFeel.installProperty(b, "opaque", Boolean.FALSE);
			}

			@Override
			public void paint(Graphics g, JComponent c) {
				g.drawImage(new ImageIcon(IntelligentMonitoring.class.getResource("/images/mainJFrame/xialaanniu.png"))
						.getImage(), 0, 2, c.getWidth() - 2, c.getHeight() - 2, c);
			}
		});
		periodTextField.add(comboBox_4);

		selectButton = new JButton("查  询");
		selectButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
		selectButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		selectButton.setForeground(Color.WHITE);
		selectButton.setBackground(Color.RED);
		selectButton.setBounds(745, 15, 100, 30);

		add(selectButton);

		JLabel label_5 = new JLabel("查询用时:");
		label_5.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label_5.setBounds(488, 48, 74, 30);
		add(label_5);

		selectInfoLabel = new JLabel("请点击查询");
		selectInfoLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
		selectInfoLabel.setForeground(Color.RED);
		selectInfoLabel.setBounds(563, 48, 126, 30);
		add(selectInfoLabel);

		if (!Config.hezuoQQqun.equals(" ")) {
			JLabel hintLabel = new JLabel(Config.hezuoQQqun);
			hintLabel.setFont(new Font("微软雅黑", Font.BOLD, 14));
			hintLabel.setBounds(689, 48, 184, 30);
			hintLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
			hintLabel.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					HttpGetUtil.openBrowseURL(Config.heziQQqunUrl);
				}
			});
			add(hintLabel);
		}

		// 定义表格列名数组
		String[] columnNames = { "商品ID", "期数", "幸运码", "中奖人", "中奖时购买人次", "中奖位", "购买区间" };
		// 定义表格数据数组
		String[][] tableValues = new String[][] { {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} };
		// 创建指定表格列名和表格数据的表格模型类的对象
		DefaultTableModel tableModel = new DefaultTableModel(tableValues, columnNames) {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};

		table = new JTable(tableModel);
		table.setSelectionForeground(Color.BLACK);
		table.setSelectionBackground(Color.CYAN);
		table.setBorder(null);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.getTableHeader().setFont(new Font("微软雅黑", Font.PLAIN, 14));
		table.setSize(838, 430);
		table.setLocation(3, 27);
		table.setForeground(Color.RED);
		add(table);
		table.setRowHeight(25);
		table.getColumnModel().getColumn(0).setPreferredWidth(20);
		table.getColumnModel().getColumn(1).setPreferredWidth(20);
		table.getColumnModel().getColumn(2).setPreferredWidth(30);
		table.getColumnModel().getColumn(3).setPreferredWidth(100);
		table.getColumnModel().getColumn(4).setPreferredWidth(40);
		table.getColumnModel().getColumn(5).setPreferredWidth(30);
		table.getColumnModel().getColumn(6).setPreferredWidth(30);

		// 设置table表头居中
		((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
		// 设置table内容居中
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
		tcr.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, tcr);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getButton() == MouseEvent.BUTTON3) {// 右键点击
					try {
						DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
						int selectedRow = table.getSelectedRow();
						String goodsID = tableModel.getValueAt(selectedRow, 0).toString();
						MyLog.outLog("右击了", goodsID);
						if (goodsID != null && !goodsID.equals("")) {
							String period = tableModel.getValueAt(selectedRow, 1).toString();
							String codeRNO = tableModel.getValueAt(selectedRow, 2).toString();
							String userName = tableModel.getValueAt(selectedRow, 3).toString();
							ShowMyMenu.ShowForeknowRightClickMenu(table,
									new String[] { goodsID, codeRNO, userName, period }, arg0.getX(), arg0.getY());
						}
					} catch (Exception e) {
					}
				}
			}
		});

		scrollPane = new JScrollPane(table);
		scrollPane.setBorder(null);
		scrollPane.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		scrollPane.setBounds(35, 86, 838, 408);
		add(scrollPane);

		verifyWinCheckBox = new JCheckBox("验证自己中奖");
		verifyWinCheckBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (verifyWinCheckBox.isSelected()) {
					if (!YunGouLoginServer.getUserServer().getIsLogin()) {
						HintDialog.startExpiringHint(HintDialog.yungouLogin, "您还未登录1元云购账户，请先登录后再使用验证中奖功能，谢谢！！！");
						verifyWinCheckBox.setSelected(false);
					} else {
						((DefaultTableModel) table.getModel()).addColumn("是否中奖");
					}
				} else {
					((DefaultTableModel) table.getModel()).setColumnCount(7);
				}
			}
		});
		verifyWinCheckBox.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		LookAndFeel.installProperty(verifyWinCheckBox, "opaque", Boolean.FALSE);
		verifyWinCheckBox.setBounds(380, 15, 120, 30);
		add(verifyWinCheckBox);

		latelyPeriodComboBox = new JComboBox<String>(new DefaultComboBoxModel<String>(new String[] { "最近期数", "精确查询" }));
		latelyPeriodComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		latelyPeriodComboBox.setSelectedIndex(0);
		latelyPeriodComboBox.setBounds(524, 15, 90, 30);
		latelyPeriodComboBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					if (latelyPeriodComboBox.getSelectedIndex() == 0) {
						periodTextField.setEnabled(false);
						selectNumComboBox.setEnabled(true);
					} else if (latelyPeriodComboBox.getSelectedIndex() == 1) {
						periodTextField.setEnabled(true);
						selectNumComboBox.setEnabled(false);
					}
				}
			}
		});
		add(latelyPeriodComboBox);

		selectNumComboBox = new JComboBox<String>(new DefaultComboBoxModel<String>(new String[] { "最近2", "最近3", "最近4",
				"最近5", "最近6", "最近7", "最近8", "最近9", "最近10", "最近11", "最近12", "最近13", "最近14", "最近15" }));
		selectNumComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		selectNumComboBox.setMaximumRowCount(14);
		selectNumComboBox.setSelectedIndex(1);
		selectNumComboBox.setBounds(632, 15, 80, 30);
		add(selectNumComboBox);

		goodsNameLblid = new JLabel("请请输入商品ID");
		goodsNameLblid.setCursor(new Cursor(Cursor.HAND_CURSOR));
		goodsNameLblid.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (!goodsIdTextField.getText().equals("")) {
					HttpGetUtil.openBrowseURL("http://www.1yyg.com/products/" + goodsIdTextField.getText() + ".html");
				}
			}
		});
		goodsNameLblid.setFont(new Font("微软雅黑", Font.BOLD, 16));
		goodsNameLblid.setForeground(Color.RED);
		goodsNameLblid.setHorizontalAlignment(SwingConstants.CENTER);
		goodsNameLblid.setBounds(35, 48, 454, 30);
		add(goodsNameLblid);
	}
}
